<template>
  <div class="scan_login">
    <div class="login">
      <span class="wx">微信</span>
      <router-link class="back" to="/">首页</router-link>
      <div class="login_form">
        <p v-if="status!==2">扫码登录</p>
        <p v-else>请在手机上确认登录</p>
        <div class="img">
          <div v-if="status===2" class="mask">
            <span class="success">扫码成功</span>
            <img class="logo" src="@/assets/img/success.png">
          </div>
          <div v-if="isLose" class="mask_white">
            <span style="color: red">登录二维码已失效</span>
            <span class="refresh" @click="getBase64QrCode">刷新一下</span>
          </div>
          <img v-if="baseImg" class="qrCode" :src="'data:image/png;base64,'+baseImg">
          <svg v-else viewBox="25 25 50 50">
            <circle r="20" cy="50" cx="50"></circle>
          </svg>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {getUUID} from "@/util/uuid.js";
import {getLoginState, getQrCode, login} from "@/api/login/index"
import {onMounted, onUnmounted, ref} from "vue";
import router from "@/router/index.js";

  let uuid = ref("")
  let baseImg = ref("")
  let status = ref(0)
  let task = ref()
  let isLose = ref(false)

  function clear(){
    clearTimeout(task.value)
  }
  function getBase64QrCode(){
    isLose.value = false
    uuid.value = getUUID()
    getQrCode({'uuid':uuid.value}).then(res=>{
      baseImg.value = res.data.data
      task.value = setInterval(()=>{
        getLoginStatus()
      },1000);
      // 四分钟后登录二维码失效
      setTimeout(()=>{
        clear()
        isLose.value = true;
      },1000*60*3);
    })
  }


function getLoginStatus(){
    getLoginState({'uuid':uuid.value}).then(res=>{
      status.value = res.data.data
      if (status.value===3){
        clearInterval(task.value)
      //   登录操作
        login({"loginType":1}).then(res=>{
          localStorage.setItem("wx_token",res.data.data)
          // 跳转到首页
          router.push("/wx/chat")
        })
      }
    })
  }

  onMounted(()=>{
      getBase64QrCode();
  })

  onUnmounted(()=>{
    if (task.value){
      clearInterval(task.value)
    }
  })

</script>

<style lang="less" scoped>
  .scan_login{
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-image: linear-gradient(to top, #fff1eb 0%, #ace0f9 100%);

  }
  svg {
    margin-top: 40px;
    width: 4.25em;
    transform-origin: center;
    animation: rotate4 2s linear infinite;
  }

  circle {
    fill: none;
    stroke: hsl(214, 97%, 59%);
    stroke-width: 2;
    stroke-dasharray: 1, 200;
    stroke-dashoffset: 0;
    stroke-linecap: round;
    animation: dash4 1.5s ease-in-out infinite;
  }

  @keyframes rotate4 {
    100% {
      transform: rotate(360deg);
    }
  }

  @keyframes dash4 {
    0% {
      stroke-dasharray: 1, 200;
      stroke-dashoffset: 0;
    }

    50% {
      stroke-dasharray: 90, 200;
      stroke-dashoffset: -35px;
    }

    100% {
      stroke-dashoffset: -125px;
    }
  }

  .login{
    position: relative;
    width: 280px;
    height: 280px;
    background-color: rgba(255,255,255,0.4);
    border-radius: 10px;
    box-shadow: 0 0 18px #c5e9ec;
    padding-top: 50px;
  }
  .login_form{
    display: flex;
    flex-direction: column;
    align-items: center;
    p{
      color: #07c160;
    }
  }
  .qrCode{
    width: 150px;
    height: 150px;
  }
  .wx{
    position: absolute;
    top: 10px;
    left: 10px;
    color: #aeadb1;
  }
  .back{
    position: absolute;
    top: 10px;
    right: 10px;
    color: #2ed937;
  }
  .img{
    position: relative;
    width: 150px;
    height: 150px;
  }
  .mask{
    border-radius: 8px;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    background-color: rgba(0,0,0,0.8);
    .success{
      color: #2ed937;
    }
    .logo{
      width: 30%;
      height: 30%;
    }
  }
  .refresh{
    color: #646cff;
    cursor: pointer;
  }
  .refresh:hover{
    color: #9ca0f8;
  }
  .mask_white{
    border-radius: 8px;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    background-color: rgba(0,0,0,0.8);
  }
</style>